﻿using System;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
namespace Services.Maps
{
    public class AMapLayer : TiledMapServiceLayer
    {
        private readonly Random random = new Random();
        public override void Initialize()
        {
            this.FullExtent = new Envelope(-20037508.342787, -20037508.342787, 20037508.342787, 20037508.342787);
            {
                SpatialReference = new ESRI.ArcGIS.Client.Geometry.SpatialReference(102100);
            };

            this.SpatialReference = new SpatialReference(102100);

            this.TileInfo = new TileInfo()
            {
                Height = 256,
                Width = 256,

                Origin = new MapPoint(-20037508.342787, 20037508.342787)
                {
                    SpatialReference = new SpatialReference(102100)
                },
                Lods = new Lod[20]
            };


            double resolution = 156543.033928;

            for (int i = 0; i < TileInfo.Lods.Length; i++)
            {

                TileInfo.Lods[i] = new Lod() { Resolution = resolution };
                resolution /= 2;
            }

            // Call base initialize to raise the initialization event 
            base.Initialize();
        }
        public override string GetTileUrl(int level, int row, int col)
        {
            string baseUrl = "http://webrd0{0}.is.autonavi.com/appmaptile?x={1}&y={2}&z={3}&lang=zh_cn&size=1&scale=1&style=7";
            var v = random.Next(1, 5).ToString();
            return string.Format(baseUrl, v, col, row, level);
        }
    }
}